package cn.wolfcode.luowowo.mgrsite.job;


import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.DetailVo;
import cn.wolfcode.luowowo.article.vo.TravelVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisticsService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisticsService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.common.key.RedisKey;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisticsService strategyStatisticsService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelStatisticsService travelStatisticsService;

    @Reference
    private ITravelService travelService;


    @Scheduled(cron = "20 * * * * *")
    public void RedisDataPersistence(){

        System.out.println("----------执行redis数据持久化---------- ");

        System.out.println(new Date());

        //持久化攻略的统计数据
        List<StrategyStatisVO> strategyStatisVOS = strategyStatisticsService.getStrategyStatisVOsByPatter(RedisKey.STRATEGY_STATIS_VO.appendKey("*"));

        for (StrategyStatisVO strategyStatisVO : strategyStatisVOS) {

            DetailVo detailVo = new DetailVo();

            BeanUtils.copyProperties(strategyStatisVO,detailVo);

            strategyDetailService.setVO(detailVo);

        }

        //持久化游记的统计数据
        List<TravelStatisVO> travelStatisVOs = travelStatisticsService.getTravelStatisVOByPattern(RedisKey.TRAVEL_STATIS_VO.appendKey("*"));

        for (TravelStatisVO travelStatisVO : travelStatisVOs) {

            TravelVO travelVO = new TravelVO();

            BeanUtils.copyProperties(travelStatisVO,travelVO);

            travelService.setVo(travelVO);

        }



        System.out.println("----------redis数据持久化结束----------");


    }
}
